/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.command

import cool.taomu.entity.ServerInfo
import cool.taomu.mqtt.mqttv3.MqttCallback
import cool.taomu.mqtt.mqttv3.Topic
import cool.taomu.utils.h2.H2Utils
import java.util.Arrays
import org.apache.commons.lang3.SerializationUtils
import org.slf4j.LoggerFactory

@Topic(value=#["taomu/jvm/info"])
class JvmInfoMqttCallback extends MqttCallback {
	val static table = "create table if not exists jvminfo(host varchar(32) UNIQUE,port int,full_load int,cpu_core int)"
	val static insert = "insert into jvminfo(host,port,full_load,cpu_core) values(?,?,?,?)";
	val static delete = "delete from jvminfo where host=? and port=?"
	val LOG = LoggerFactory.getLogger(JvmInfoMqttCallback);

	override message(String topic, byte[] message) {
		var info = SerializationUtils.deserialize(message) as ServerInfo;
		LOG.info("jvm info mqtt call back:{}", info.toString());
		var h2 = new H2Utils();
		h2.modify(table);
		h2.modify(delete, #[info.host, info.port].toArray);
		h2.modify(insert, #[info.host, info.port, info.runtimeInfo.fullLoad, info.runtimeInfo.cpuCore].toArray);
		return null;
	}

}
